package com.artislong.text2sql;

import lombok.AllArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.stereotype.Service;

/**
 *
 * @author 陈敏
 * @version SqlGenerator.java, v 1.0 2025 07 02 23:17 chenmin Exp $
 * Created on 2025 07 02
 */
@Service
@AllArgsConstructor
public class SqlGenerator {

    private final ChatClient chatClient;

    public String generate(String question) {
        String response = chatClient
                .prompt(question)
                .call()
                .content();

        boolean isSelectQuery = response.startsWith("SELECT");
        if (Boolean.FALSE.equals(isSelectQuery)) {
            throw new RuntimeException(response);
        }
        return response;
    }
}
